package com.cloudlead.common.lang.persistence;

import com.cloudlead.common.lang.annotation.Label;

import javax.validation.Valid;

import java.io.Serializable;
import java.util.*;

/**
 * 搜索过滤条件
 *
 * @author dean.lu
 */
@Label("搜索与条件")
public final class Criteria extends AbstractList<Term> implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 2775559535419197826L;

    @Valid
    @Label("与条件")
    private List<Term> and;

    public Criteria() {
        and = new ArrayList<>();
    }

    public Criteria(Term... and) {
        this(Arrays.asList(and));
    }

    public Criteria(List<Term> and) {
        if (null == and || and.isEmpty()) {
            throw new IllegalArgumentException(
                    "You have to provide at least one search property to search!");
        }
        this.and = and;
    }

    @Override
    public void add(int index, Term element) {
        and.add(index, element);
    }

    @Override
    public Term remove(int index) {
        return and.remove(index);
    }

    @Override
    public Iterator<Term> iterator() {
        return and.iterator();
    }

    @Override
    public int size() {
        return and.size();
    }

    @Override
    public Term get(int index) {
        return and.get(index);
    }

    public List<Term> getAnd() {
        return and;
    }

    public void setAnd(List<Term> and) {
        this.and = and;
    }

    public Criteria and(Term term) {
        and.add(term);
        return this;
    }

    @Override
    public String toString() {
        return "Criteria{" +
                "and=" + and +
                '}';
    }
}
